package com.example.demo.model.admin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.Results.ResultClass;
import com.example.demo.model.admin.admin.entity.ShopAdmin;
import com.example.demo.model.admin.admin.entity.eid.UpdatePasswd;
import com.example.demo.model.admin.admin.entity.reg.LoginReq;
import com.example.demo.model.admin.admin.mapper.ShopAdminDao;
import com.example.demo.model.admin.admin.service.ShopAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ShopAdminServiceImpl extends ServiceImpl<ShopAdminDao, ShopAdmin> implements ShopAdminService {

    @Autowired
    ShopAdminDao shopAdminDao;

    @Override
    public ResultClass doLogin(HttpServletRequest request, LoginReq loginReq) {

       /* String random = (String) request.getSession().getAttribute("RANDOMVALIDATECODEKEY");
        if (!random.toLowerCase().equals(loginReq.getCode().toLowerCase())) {
            return ResultClass.fail("验证码错误");
        }*/
        String md5Password = DigestUtils.md5DigestAsHex(loginReq.getAdminPass().getBytes());
        loginReq.setAdminPass(md5Password);


        // 1 使用sql语句判断
       // List<ShopAdmin> list =shopAdminDao.selectUserByNameAndPass(loginReq);

        // 2 使用mybatis-plus方式

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("admin_name", loginReq.getAdminName());
        queryWrapper.eq("admin_pass", loginReq.getAdminPass());

       /* QueryWrapper<ShopAdmin> queryWrapper = new QueryWrapper<ShopAdmin>();
        queryWrapper.eq("admin_name",loginReq.getAdminName());
        queryWrapper.lambda(). eq(ShopAdmin::getAdminName,loginReq.getAdminName());*/

        List<ShopAdmin> list = shopAdminDao.selectList(queryWrapper);

        if(list.size()!=1){
            return ResultClass.fail("用户名或密码错误");
        }

        request.getSession().setAttribute("adminId", list.get(0).getId());
        request.getSession().setAttribute("adminName", loginReq.getAdminName());
        return ResultClass.ok("登陆成功");
    }

    @Override
    public ResultClass doReg(HttpServletRequest request, LoginReq loginReq) {

        String random = (String) request.getSession().getAttribute("RANDOMVALIDATECODEKEY");
        if (!random.toLowerCase().equals(loginReq.getCode().toLowerCase())) {
            return ResultClass.fail("验证码错误");
        }

        String md5Password = DigestUtils.md5DigestAsHex(loginReq.getAdminPass().getBytes());
        loginReq.setAdminPass(md5Password);

        int count = shopAdminDao.selectUserByName(loginReq);
        if(count >= 1){
            return ResultClass.fail("用户名重复");
        }
        shopAdminDao.insertUser(loginReq);

        return ResultClass.ok("注册成功");
    }

    @Override
    public ResultClass editPasswd(HttpServletRequest request, UpdatePasswd updatePasswd) {

        String md5Password = DigestUtils.md5DigestAsHex(updatePasswd.getAdminPass().getBytes());
        updatePasswd.setAdminPass(md5Password);


        String oldPasswd = shopAdminDao.selectByIdAndPasswd(updatePasswd);

        String adminPass = DigestUtils.md5DigestAsHex(updatePasswd.getNewpwd().getBytes());
        updatePasswd.setNewpwd(adminPass);


        if(!oldPasswd.equals(updatePasswd.getAdminPass())){
            return ResultClass.fail("原密码错误！");
        }


      /*

       Integer AdminId = (Integer) request.getSession().getAttribute("adminId".toString());

      ShopAdmin shopAdmin = new ShopAdmin();
        shopAdmin.setId(adminId);
        shopAdmin.setAdminPass(newPwd);
        shopAdmin.updateById(shopAdmin);
        shopAdminDao.updateById(shopAdmin);
        return ResultClass.ok("修改密码成功！");
        */


        shopAdminDao.updateByUserPasswd(updatePasswd);

        return ResultClass.ok("修改密码成功！");
    }

    @Override
    public ModelAndView editPass() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("#");
        return modelAndView;
    }



}


